package com.siqi.hr.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import com.siqi.hr.common.Constants;
import com.siqi.hr.system.common.utils.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Neil
 */
public class FileUtils {
    
    private static Log log = LogFactory.getLog(FileUtils.class);

    /**
     * 创建目录及文件
     * @author xiaogq 3013-07-26
     * @param  delpath     文件夹路径
     * @return boolean
     */
    public static void createFile(String delpath) {
        File file = new File(delpath);
        if(file.isDirectory()){
            if (!file.exists()) {//如果文件夹或目录不存在则创建
                file.mkdirs();
            }
        }
    }

    /**
     * 传入File类型参数的文件复制
     *@author xiaogq  2017-01-06
     *@param  file    File 对象
     *@param  url     文件目的路径
     *void
     */
    public static void copy(File file ,String url){
        try {
            FileOutputStream fos = new FileOutputStream(url);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024*1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 传入BYTE数组的文件复制
     *@author  xiaogq  2017-01-06
     *@param   buffer  BYTE数组
     *@param   url    目的参数
     *void
     */
    public static void copy(byte[] buffer, String url){
        try {
            //copy文件时先删除当前路径下的文件在复制
            File file = new File(url);
            file.delete();
            //复制文件
            FileOutputStream fos = new FileOutputStream(url);
            fos.write(buffer);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除某个文件夹下的所有文件夹和文件
     * @author xiaogq 3013-07-26
     * @param  delpath     文件夹路径
     * @param  filename    文件名称
     * @return boolean
     */
    public static void deletefile(String delpath,String filename) {
        try {
            File file = new File(delpath);
            if (!file.isDirectory()) {//如果文件不是目录
                if(file.exists()){
                    file.delete();
                }
            }else if (file.isDirectory()) {//如果文件是目录
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + "\\" + filelist[i]);
                    if(!delfile.equals(filename)){
                        if(delfile.exists()){
                            delfile.delete();
                        }
                    }
                }
            }
        }catch (Exception e) {
            e.getStackTrace();
        }
    }

    public static boolean writeFile(String filePath, String fileName, byte[] contents) {
        String location = Constants.UPLOAD_FILE_LOCATION + File.separator + filePath;
        File file = new File(location);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            File imgFile = new File(location + File.separator + fileName);
            org.apache.commons.io.FileUtils.writeByteArrayToFile(imgFile, contents);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("error when writing the file to the server: " + fileName, e.getCause());
            return false;
        }
        return true;
    }

    /**
     * 处理文件上传
     * @param file
     * @return
     */
    public static String uploadFiles(MultipartFile file) {
        String fileLocation = "";
        try {
            String originalFileName = file.getOriginalFilename();
            String saveFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.lastIndexOf("."));
            String filePath = "custom" + File.separator + DateUtil.formatDate(new Date(),"yyyy-MM-dd");
            if (FileUtils.writeFile(filePath, saveFileName, file.getBytes())) {
                fileLocation = filePath.replace(File.separator, "/") + "/" + saveFileName;
            } else {
                System.out.println("error when uploading data file");
            }
        } catch (Exception e) {
            System.out.println("error when uploading data file");
        }
        return fileLocation;
    }

}
